package com.colabo.j2ee.web.oaf.security;

import java.util.Collection;

import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import com.colabo.j2ee.web.core.context.ContextUtil;
import com.colabo.j2ee.web.oaf.security.model.AdminAuthorityModel;


public class AuthUtil {
	public static final String URL_PREFIX = "U_";
	public static final String MENU_PREFIX = "M_";

	public static boolean isAdmin(Authentication authentication) {
		Collection<GrantedAuthority> auths = authentication.getAuthorities();
		for (GrantedAuthority grantedAuthority : auths) {
			if (grantedAuthority instanceof AdminAuthorityModel) {
				return true;
			}
		}
		return false;
	}
	
	// retrieve acegi encoder setting from acegi xml
	public static String encodePassword(String password, String salt) {
		PasswordEncoder passwordEncoder = (PasswordEncoder) ContextUtil.getBean("passwordEncoder");
		return passwordEncoder.encodePassword(password, salt);
	}

	/**
	 * 加密
	 */
	public static String encodePassword(String password) {
		return encodePassword(password, null);
	}
}
